﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.NOTICE;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;

public partial class EditNotice : System.Web.UI.Page
{
    /// <summary>
    /// 通知操作对象
    /// </summary>
    private Notice_DAO noticeDAO = new Notice_DAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
        if(!this.IsPostBack)
        {
            this.InitData();
        }
    }

    /// <summary>
    /// 清空信息
    /// </summary>
    private void InitData()
    {
        //获取通知的ID
        String idString = Request["id"];
        //获取类型，用于判断是否为编辑
        String type = Request["type"];

        int id = 0;
        try
        {
            id = Int32.Parse(idString);
        }
        catch (Exception)
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        Notice notice = this.noticeDAO.SelectNOTICEByID(id);
        if (null == notice){
      
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=错误：没有找到您要看的通知！！！");
            return;
        }
        this.titleTextBox.Text =notice.title;
        this.contentTextBox.Text = notice.content;
        this.dlrLabel.Text = notice.dlr;
        this.visit_count_Label.Text = notice.visit_count.ToString();
        this.submit_time_Label.Text = notice.submit_time.ToString();
        this.idHiddenField.Value = notice.id.ToString();
        if ("edit".Equals(type))
        {
            this.SetVisible(true);
        }
        else
        {
            this.SetVisible(false);
            this.noticeDAO.UpdateNOTICE_VISIT_COUNT(notice);
            this.contentTextBox.Height = Unit.Pixel(600);
        }
    }

    /// <summary>
    /// 验证标题
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateTitle()
    {
        Boolean flag = true;
        this.errorLabel.Text = "";
        this.titleTextBox.Text = this.titleTextBox.Text.Trim();
        if ("".Equals(this.titleTextBox.Text))
        {
            this.errorLabel.Text = "错误：标题不能为空";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 获取通知对象
    /// </summary>
    /// <returns></returns>
    private Notice GetNotice()
    {
        Notice notice = new Notice();
        notice.title = this.titleTextBox.Text;
        notice.content = this.contentTextBox.Text;
        try
        {
            notice.id = Int32.Parse(this.idHiddenField.Value);
        }
        catch (Exception)
        {
        }
        return notice;
    }
    /// <summary>
    ///  验证内容
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateContent()
    {
        Boolean flag = true;
        this.errorLabel.Text = "";
        this.contentTextBox.Text = this.contentTextBox.Text.Trim();
        if ("".Equals(this.contentTextBox.Text))
        {
            this.errorLabel.Text = "错误：通知内容不能为空";
            flag = false;
        }
        return flag;
    }
    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (!this.ValidateTitle())
        {
            return;
        }
        if (!this.ValidateContent())
        {
            return;
        }
        Notice notice = this.GetNotice();
        Boolean flag=this.noticeDAO.UpdateNOTICE(notice);
        if (flag)
        {
            Response.Write("<Script>alert('修改通知成功')</Script>");
        }
        else
        {
            Response.Write("<Script>alert('修改通知失败')</Script>");
        }
    }
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.InitData();
    }

    /// <summary>
    /// 设置控件的可见性,包括标题标签、内容标签、提交按钮、重置按钮
    /// </summary>
    /// <param name="b"></param>
    private void SetVisible(Boolean b)
    {
        this.submitButton.Visible = b;
        this.resetButton.Visible = b;
        this.titleTextBox.Enabled = b;
        this.contentTextBox.Enabled = b;
        if (b)
        {
            this.titleTextBox.BorderStyle = BorderStyle.NotSet;
            this.contentTextBox.BorderStyle = BorderStyle.NotSet;
        }
        else
        {
            this.titleTextBox.BorderStyle = BorderStyle.None;
            this.contentTextBox.BorderStyle = BorderStyle.None;
        }
    }
}